<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of mealController
 *
 * @author Administrator
 */
global $config;
require_once ($config['base_dir'] . "/core/zhanggui_db_tool.php");

class mealController {
    //put your code here
    function index()
    {        
        $config['title'] = "zhanggui meal page";
        $config['keywords'] = "zhanggui";
        $config['description'] = "zhanggui meal list";
        $config['favicon'] = "/zhanggui/theme/default/favicon.ico";
        
        register("mealdoc", $this->mealdata(8), "getMealTable");
        //$mealdata = mealdata(8);
    }
    
    function mealdata($count)
    {
        $meal_db = new zhangguiDBTool();
        $mealcount = $meal_db->getAllMealCount();
        
        $index = 0;
        $curPage = 1;
        if(isset($_GET['page']))
        {
            $curPage = $_GET['page'] ;
            $index = ($curPage - 1)*$count;
        }        
        
        $allPage = (int)($mealcount / $count);
        if(($mealcount % $count) != 0)
        {
            $allPage += 1;
        }
        
        if($mealcount < $index)
        {
            $index = ($allPage - 1)*$count;
        }
        else if($index < 0)
        {
            $index = 0;
        }
        
        $meals = $meal_db->getMeals($index, $count);
        
        if($curPage < 1)
        {
            $curPage = 1;
        }
        else if($curPage > $allPage)
        {
            $curPage = $allPage;
        }
        
        $fpage .= "&nbsp&nbsp<b>当前页：第 $curPage 页</b>&nbsp&nbsp";
        $fpage .= $this->gennerateDirctionPageHref($curPage, $allPage);
        $fpage .= "&nbsp&nbsp<b>总页数：$allPage 页</b>&nbsp&nbsp";
        
        $fpage .= $this->generateFirstPageHref($curPage);
        $fpage .= $this->generatePrePageHref($curPage);
        $fpage .= $this->generateNextPageHref($curPage, $allPage);
        $fpage .= $this->generateLastPageHref($curPage, $allPage);
        
        //echo get_cur_uri(array("key"=>"value", "key2"=>"value2"));
        
        return array('count'=>$mealcount, 'meals'=>$meals, 'fpage'=>$fpage);
    }
    
    function generatePrePageHref($curPage)
    {
        $href = "";
        
        if($curPage > 1)
        {
            $paraArray = array("a"=>"meal", "page"=>$curPage-1);
            $link = get_cur_uri($paraArray);
            $href = "&nbsp<a href=$link>上一页</a>&nbsp";
        }
        
        return $href;
    }
    
    function generateNextPageHref($curPage, $allPage)
    {
        $href = "";
        if($curPage < $allPage)
        {
            $paraArray = array("a"=>"meal", "page"=>$curPage+1);
            $link = get_cur_uri($paraArray);
            $href = "&nbsp<a href=$link>下一页</a>&nbsp";
        }
        
        return $href;
    }
    
    function generateTrunPrePageHref($curPage)
    {
        $href = "";
        
        if($curPage > 1)
        {
            $paraArray = array("a"=>"meal", "page"=>$curPage-1);
            $link = get_cur_uri($paraArray);
            $href = "&nbsp<a href=$link>"."<"."</a>&nbsp";
        }
        
        return $href;
    }
    
    function generateTrunNextPageHref($curPage, $allPage)
    {
        $href = "";
        if($curPage < $allPage)
        {
            $paraArray = array("a"=>"meal", "page"=>$curPage+1);
            $link = get_cur_uri($paraArray);
            $href = "&nbsp<a href=$link>".">"."</a>&nbsp";
        }
        
        return $href;
    }
    
    function generateNumHref($num, $allPage)
    {
        $href = "";
        if($num > 0 && $num <= $allPage)
        {
            $paraArray = array("a"=>"meal", "page"=>$num);
            $link = get_cur_uri($paraArray);
            $href = "&nbsp<a href=$link>$num</a>&nbsp";
        }
        
        return $href;
    }
    
    function generateFirstPageHref($curPage)
    {
         $href = "";
        
        if($curPage > 1)
        {
            $paraArray = array("a"=>"meal", "page"=>1);
            $link = get_cur_uri($paraArray);
            $href = "&nbsp<a href=$link>首页</a>&nbsp";
        }
        
        return $href;
    }
    
    function generateLastPageHref($curPage, $allPage)
    {
         $href = "";
        
        if($curPage < $allPage)
        {
            $paraArray = array("a"=>"meal", "page"=>$allPage);
            $link = get_cur_uri($paraArray);
            $href = "&nbsp<a href=$link>尾页</a>&nbsp";
        }
        
        return $href;
    }
    
    function generateTurnFirstPageHref($curPage)
    {
         $href = "";
        
        if($curPage > 1)
        {
            $paraArray = array("a"=>"meal", "page"=>1);
            $link = get_cur_uri($paraArray);
            $href = "&nbsp<a href=$link>"."<<"."</a>&nbsp";
        }
        
        return $href;
    }
    
    function generateTurnLastPageHref($curPage, $allPage)
    {
         $href = "";
        
        if($curPage < $allPage)
        {
            $paraArray = array("a"=>"meal", "page"=>$allPage);
            $link = get_cur_uri($paraArray);
            $href = "&nbsp<a href=$link>".">>"."</a>&nbsp";
        }
        
        return $href;
    }
    
    function gennerateDirctionPageHref($curPage, $allPage)
    {
        $directions = "";
        $allDirctions = 8;
        $i = 0;
        
        if($allDirctions > $allPage)
        {
            $allDirctions = $allPage;
        }
        
        $directions .= $this->generateTurnFirstPageHref($curPage);
        $directions .= $this->generateTrunPrePageHref($curPage);
        
        $middleIndex = (int)($allDirctions/2);
        $i = $curPage - $middleIndex;
        for($count = 0; $count < $middleIndex; $count++,$i++)
        {
            if($i > 0)
            {
                $directions .= $this->generateNumHref($i, $allPage);
            }
        }
        
        $directions .= "&nbsp".$curPage."&nbsp";
        
        $i = $curPage + 1;
        
        for($count = 0; $count < $allDirctions; $i++, $count++)
        {
            if($i > $allPage)
                break;
            
            $directions .= $this->generateNumHref($i, $allPage);
        }
        
        $directions .= $this->generateTrunNextPageHref($curPage, $allPage);
        $directions .= $this->generateTurnLastPageHref($curPage, $allPage);
        
        return $directions;
    }
    
    function gennerateGoPageCode($curPage, $allPage)
    {
        
    }
}

?>
